package com.example.safeassistan.home.activity;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import androidx.core.content.PermissionChecker;
import com.example.safeassistan.R;
import com.example.safeassistan.base.BaseActivity;
import com.yzq.zxinglibrary.bean.ZxingConfig;
import com.yzq.zxinglibrary.common.Constant;

public class ImmediatelyPatrol extends BaseActivity implements View.OnClickListener {
    private static final int REQUEST_CODE = 101;
    private int REQUEST_CODE_SCAN=102;
    private TextView tvQrCode,tvNfc;
    private ImageView ivBack;

    public static void actionStart(Context context) {
        Intent intent = new Intent(context, ImmediatelyPatrol.class);
        context.startActivity(intent);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tvNfc:
                reSetStatus();
                tvNfc.setBackgroundResource(R.drawable.leftcorner_blue_bg_5);
                tvNfc.setTextColor(getResources().getColor(R.color.white));
                break;
            case R.id.tvQrCode:
                reSetStatus();
                tvQrCode.setBackgroundResource(R.drawable.rightcorner_blue_bg);
                tvQrCode.setTextColor(getResources().getColor(R.color.white));
                checkPermission();
                break;
        }
    }

    /****权限**/
    private void checkPermission(){
        try{
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                if (PermissionChecker.checkSelfPermission(ImmediatelyPatrol.this, Manifest.permission.CAMERA)!= PermissionChecker.PERMISSION_GRANTED) {
                    // 不相等 请求授权
                    ActivityCompat.requestPermissions(ImmediatelyPatrol.this, new String[]{Manifest.permission.CAMERA
                    },REQUEST_CODE);
                }else {
                    //扫描二维码
                    //二维码扫一扫
                    Intent intent = new Intent(getContext(), CaptureActivity.class);
                    intent.putExtra("riskPointId","");
                    intent.putExtra("defId","");
                    intent.putExtra("posi",0);
                    /**ZxingConfig是配置类
                     *可以设置是否显示底部布局，
                     * 闪光灯，相册 是否播放提示音  震动
                     * 设置扫描框颜色等 也可以不传这个参数
                     * */
                    ZxingConfig config = new ZxingConfig();
                    config.setPlayBeep(true);//是否播放扫描声音 默认为true
                    config.setShake(true);//是否震动  默认为true
                    config.setDecodeBarCode(true);//是否扫描条形码 默认为true
                    config.setReactColor(R.color.blue_3B5BEA);//设置扫描框四个角的颜色 默认为白色
                    config.setFrameLineColor(R.color.blue_3B5BEA);//设置扫描框边框颜色 默认无色
                    config.setScanLineColor(R.color.blue_3B5BEA);//设置扫描线的颜色 默认白色
                    config.setFullScreenScan(false);//是否全屏扫描  默认为true  设为false则只会在扫描框中扫描
                    config.setShowFlashLight(true);
                    intent.putExtra(Constant.INTENT_ZXING_CONFIG, config);
                    startActivityForResult(intent, REQUEST_CODE_SCAN);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        setAndroidNativeLightStatusBar(true);
    }

    @Override
    protected int setLayoutResourceID() {
        return R.layout.immediately_patrol;
    }

    @Override
    protected void initView() {
        tvQrCode=findViewById(R.id.tvQrCode);
        tvNfc=findViewById(R.id.tvNfc);
        ivBack=findViewById(R.id.ivBack);
        tvQrCode.setOnClickListener(this::onClick);
        ivBack.setOnClickListener(this::onClick);
        tvNfc.setOnClickListener(this::onClick);
    }

    @SuppressLint("MissingSuperCall")
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode) {
            case REQUEST_CODE:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    Log.d("aaa", "onRequestPermissionsResult: " +
                            "权限申请成功");
                    for (int i : grantResults) {
                        Log.d("aaa", "onRequestPermissionsResult: " + i);
                    }
                    //扫码开始
                    //二维码扫一扫
                    Intent intent = new Intent(getContext(), CaptureActivity.class);
                    intent.putExtra("riskPointId","");
                    intent.putExtra("defId","");
                    intent.putExtra("posi",0);
                    /**ZxingConfig是配置类
                     *可以设置是否显示底部布局，
                     * 闪光灯，相册 是否播放提示音  震动
                     * 设置扫描框颜色等 也可以不传这个参数
                     * */
                    ZxingConfig config = new ZxingConfig();
                    config.setPlayBeep(true);//是否播放扫描声音 默认为true
                    config.setShake(true);//是否震动  默认为true
                    config.setDecodeBarCode(true);//是否扫描条形码 默认为true
                    config.setShowFlashLight(true);
                    config.setReactColor(R.color.blue_3B5BEA);//设置扫描框四个角的颜色 默认为白色
                    config.setFrameLineColor(R.color.blue_3B5BEA);//设置扫描框边框颜色 默认无色
                    config.setScanLineColor(R.color.blue_3B5BEA);//设置扫描线的颜色 默认白色
                    config.setFullScreenScan(false);//是否全屏扫描  默认为true  设为false则只会在扫描框中扫描
                    intent.putExtra(Constant.INTENT_ZXING_CONFIG, config);
                    startActivityForResult(intent, REQUEST_CODE_SCAN);
                } else {
                    Toast.makeText(ImmediatelyPatrol.this, "您已拒绝申请权限", Toast.LENGTH_SHORT).show();
                }
                break;
            default:
                break;
        }
    }

    public void reSetStatus(){
        tvNfc.setBackground(null);
        tvQrCode.setBackground(null);
        tvNfc.setTextColor(getResources().getColor(R.color.blue_3B5BEA));
        tvQrCode.setTextColor(getResources().getColor(R.color.blue_3B5BEA));
    }
    @Override
    protected void initData() { }

    @Override
    protected int getTitleBarType() {
        return TITLEBAR_NULL;
    }
}